export default {
  data() {
    return {
      pageProp: {
        total: 0,
        pageSize: 10,
        pageSizes: [10, 20, 50, 100],
        currentPage: 1,
        pageNumber: 0,
        layout: "total, sizes, prev, pager, next, jumper",
        orderBy: undefined,
        background: true,
      },
    };
  },
  methods: {
    updatePage(prop, value) {
      if (typeof prop === "object") {
        this.pageProp = prop;
        return;
      } else if (typeof prop === "string") {
        this.$set(this.pageProp, prop, value);
      }
    },
    handleCurrentChange(pageNumber) {
      this.updatePage("currentPage", pageNumber);
      this.queryTable();
    },
    handleSizeChange(pageSize) {
      this.updatePage("pageSize", pageSize);
      this.queryTable();
    },
    handleSortChange(data) {
      let { prop, order } = data;
      if (prop && order) {
        this.updatePage(
          "orderBy",
          prop + "," + (order === "ascending" ? "asc" : "desc")
        );
        this.queryTable();
      } else {
        this.updatePage("orderBy", "");
        this.queryTable();
      }
    },
    getParams() {
      return {
        pageSize: this.pageProp.pageSize,
        pageIndex: this.pageProp.currentPage,
      };
    },
    resetForm(formName) {
      this.pageProp.currentPage = 1;
      this.$refs[formName].resetFields();
      this.queryTable();
    },
  },
};
